package com.hup.utils.commons.hardware;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.prefs.Preferences;

/**
 * @author hugan
 * @date 2019/8/17
 */
public class SystemUtil {

    /**
     * @throws IllegalArgumentException 打开异常
     */
    public static void openDir(String path) {
        try {
            File dir = new File(path);
            if (!dir.exists()) {
                throw new IllegalArgumentException("不存在文件夹:" + path);
            }
            if (!dir.isDirectory() && !dir.getName().endsWith(".lnk")) {
                throw new IllegalArgumentException("非文件夹:" + path);
            }
            Desktop.getDesktop().open(dir);
        } catch (IOException e) {
            throw new IllegalArgumentException("打开错误" + e.getMessage());
        }
    }

    /**
     * 保存内容到系统粘贴板
     */
    public static void saveToClipboard(String txt) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(txt);
        clip.setContents(tText, null);
    }

    public static void disableInputMethods(Component c) {
        disableInputMethods(c, false);
    }

    /**
     * 禁用输入法;
     *
     * @apiNote 禁用后用户界面将不能开启输入法, 直到代码恢复
     */
    public static void disableInputMethods(Component c, boolean withAllChild) {
        c.enableInputMethods(false);
        if (withAllChild && c instanceof Container) {
            for (Component child : ((Container) c).getComponents()) {
                disableInputMethods(child, true);
            }
        }
    }

    public static void enableInputMethods(Component c, boolean withAllChild) {
        c.enableInputMethods(true);
        if (withAllChild && c instanceof Container) {
            for (Component child : ((Container) c).getComponents()) {
                enableInputMethods(child, true);
            }
        }
    }

    public static int cpuCount() {
        return Runtime.getRuntime().availableProcessors();
    }

    public static void beep() {
        Toolkit.getDefaultToolkit().beep();
    }

    /**
     * 从网络中获取当前时间
     *
     * @param withUsedSpan false-直接返回url链接中获取到的时间(调用方法时的时间);true-前者的时间加上建立连接时的耗时(得到本方法返回值时的时间)
     * @return notNull, 不带毫秒数的时间
     * @throws IOException io异常
     * @apiNote 关于[withUsedSpan], 没做严格的全覆盖测试, 其结果差值应该很少
     */
    public static Date getNetTime(boolean withUsedSpan) throws IOException {
        long st = System.currentTimeMillis();
        URL url = new URL("https://www.baidu.com");// 取得资源对象
        URLConnection connection = url.openConnection();// 生成连接对象
        connection.setConnectTimeout(300);
        connection.connect();// 发出连接
        long time = connection.getDate();// 读取网站日期时间

        long span = System.currentTimeMillis() - st;
        if (withUsedSpan) time += span;
        return new Date(time);
    }

    /**
     * @return 当前程序是否由管理员权限
     */
    public static boolean isAdmin() {
        try {
            Preferences preferences = Preferences.systemRoot();
            preferences.put("foo", "bar"); // SecurityException on Windows
            preferences.remove("foo");
            preferences.flush(); // BackingStoreException on Linux
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String waitForInput(byte[] buff) throws IOException {
        int readLength = System.in.read(buff, 0, buff.length);
        return new String(buff, 0, readLength - 1);//-1去掉换行符
    }

}
